<?php

namespace app\admin\controller;

use app\admin\model\LangClass;
use app\common\controller\Send;
use app\admin\model\LangClassFile AS FileModel;
use app\admin\validate\LangClassFile AS FileValidate;
use app\common\enums\StatusEnum;
use app\common\service\File;
use think\facade\Request;

class LangClassFile extends Base
{
    use Send;

    /**
     * 文件列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $search = input();
        $classId = isset($search['classId']) ? $search['classId'] : 0;
        $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
        $type = isset($search['type']) ? $search['type'] : FileModel::TYPE_VIDEO;
        $name = isset($search['file_name']) ? $search['file_name'] : '';
        $status = isset($search['status']) ? $search['status'] : -1;
        $lang = isset($search['lang']) ? $search['lang'] : '';
        $className = isset($search['className']) ? $search['className'] : '';
        $search = [
            'lang' => $lang,
            'className' => $className,
            'classId' => $classId,
            'courseType' => $courseType,
            'type' => $type,
            'file_name' => $name,
            'status' => $status
        ];

        $where = [];
        if ($search['file_name']) {
            $where[] = ['file_name', 'like', '%' . $search['file_name'] . '%'];
        }

        if ($search['status'] != null && $search['status'] != -1) {
            $where[] = ['status', '=', $search['status']];
        }

        $model = FileModel::getList($classId, $courseType, $type, $where);
        return view('index', [
            'model' => $model['model'],
            'count' => $model['count'],
            'search' => $search,
            'status' => StatusEnum::$listExplain,
            'courseName' => FileModel::attributes('course_type', $courseType),
            'typeName' => FileModel::attributes('type', $type),
        ]);
    }

    /**
     * 添加修改文件
     * @return \think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function form()
    {
        if (Request::instance()->isPost()) {
            $data = input('post.');

            // 检测变量
            $validate = new FileValidate();
            if (!$validate->check($data)) {
                return json(self::sendResult(102, [], $validate->getError()));
            }

            $fileId = FileModel::saveOne($data);
            $code = $fileId > 0 ? self::$okCode : 602;
            return json(self::sendResult($code, [
                'url' => url('index', [
                    'classId' => $data['class_id'],
                    'courseType' => $data['course_type'],
                    'type' => $data['type'],
                    'lang' => $data['lang'],
                    'className' => $data['className'],
                ]),
            ]));
        } else {
            $search = input();
            $classId = isset($search['classId']) ? $search['classId'] : 0;
            $courseType = isset($search['courseType']) ? $search['courseType'] : 0;
            $type = isset($search['type']) ? $search['type'] : FileModel::TYPE_VIDEO;
            $lang = isset($search['lang']) ? $search['lang'] : '';
            $className = isset($search['className']) ? $search['className'] : '';
            $id = isset($search['id']) ? $search['id'] : 0;
            $model = [];
            $title = '添加';
            if ($id > 0) {
                $model = FileModel::get($id);
                $title = '编辑';
            }

            if ($type == 1) {
                // 1、视频
                $uploadType = 2;
                $uploadSize = '100gb';
                $uploadFormat = 'mp3,mp4,wma,avi,rm,rmvb,flv,mpg,mov,mkv,ogv';
            } else if ($type == 2) {
                // 2、音频
                $uploadType = 2;
                $uploadSize = '100gb';
                $uploadFormat = 'mp3';
            } else {
                // 3、课件/随堂作业
                $uploadType = 3;
                $uploadSize = '100gb';
                $uploadFormat = 'zip,rar,pdf,doc,xls,xlsx';
            }
            return view('form', [
                'title' => $title,
                'status' => StatusEnum::$listExplain,
                'model' => $model,
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'lang' => $lang,
                'className' => $className,
                'courseName' => FileModel::attributes('course_type', $courseType),
                'isFree' => FileModel::attributes('is_free'),
                'typeName' => FileModel::attributes('type', $type),
                'uploadType' => $uploadType, // 2视频、音频；3课件等
                'uploadSize' => $uploadSize,
                'uploadFormat' => $uploadFormat
            ]);
        }
    }

    /**
     * 删除数据
     * @param $id
     * @return array
     */
    public function delete($id)
    {
        $model = FileModel::get($id);
        $classId = $courseType = $type = 0;
        $lang = input('lang');
        $className = input('className');
        if ($model) {
            $classId = $model['class_id'];
            $courseType = $model['course_type'];
            $type = $model['type'];
            $model->delete();
        }

        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }

    /**
     * 批量启用、禁用
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function disables()
    {
        $status = input('status');
        $ids = input('ids');
        $classId = input('classId');
        $courseType = input('courseType');
        $type = input('type');
        $lang = input('lang');
        $className = input('className');
        FileModel::disables($ids, $status);
        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }

    /**
     * 修改状态
     * @param $id
     * @return array
     */
    public function disabled($id)
    {
        $model = FileModel::get($id);
        $classId = $courseType = $type = 0;
        $lang = input('lang');
        $className = input('className');
        if ($model) {
            $classId = $model['class_id'];
            $courseType = $model['course_type'];
            $type = $model['type'];
            $model->status = ($model->status == StatusEnum::ENABLED) ? StatusEnum::DISABLED : StatusEnum::ENABLED;
            $model->update_time = time();
            $model->save();
        }
        return self::sendResult(self::$okCode, [
            'url' => url('index', [
                'classId' => $classId,
                'courseType' => $courseType,
                'type' => $type,
                'lang' => $lang,
                'className' => $className
            ]),
        ]);
    }
}
